#ifndef GAMEROOM_H
#define GAMEROOM_H

#include "main.h"
#include "card.h"
#include "player.h"
#include <list>


class GameRoom {
public:
    GameRoom(server* sver) ;
    ~GameRoom();
    void StartGame();
    string getId(){
        return roomID;
    }

    ErrorType kick(Player* player);
    ErrorType addPlayer(Player* player);

    bool isLobby(){
        return state == LOBBY;
    }
    void setIconStatus(int ic, bool status){
        iconlist[ic] = status;
    }
    bool popValidIcon(int i){
        if(iconlist[i]){
            iconlist[i] = false;
            return true;
        }
        return false;
    }

    enum MessageType{
        ROOMSTATE,
        GAMESTATE
    };

    //ErrorType notifyAll(MessageType type);
    
    ErrorType updatePlayer(string id);

    ErrorType updateReadyPlayer(string id);
    ErrorType updateUnReadyPlayer(string id);

    void playerGo(string uid);
    void playerBack(string uid);

    void askForNewRound(websocketpp::connection_hdl hdl);
    void askForLobby(websocketpp::connection_hdl hdl);
private:
    //生成ID相关
    static random_device rd;
    static mt19937 mtrand;
    string generateID();
    static const string characters;
    //卡牌相关
    static const vector<Card> allcards;
    
    static const vector<int> init_cards;

    vector<int> cards = vector<int>(30);
    static const int artifact_cards[6];
    void buildCards();
    void initCards();

    vector<int>::iterator cur_card;

    void initLobby();
    
    bool notify(websocketpp::connection_hdl hdl, const string& msg) const;
    bool notifyAll(const Json::Value& jmsg) const;
    bool notifyAll(const string& msg) const;

    server* s;
    string roomID;
    //boost::asio::deadline_timer* countdownTimer;
    unordered_map<string, Player*> players;

    enum State{
        LOBBY,
        GAMING,
        ENDING
    } state;

    Json::Value lobbyinf;
    Json::Value gameinf;
    Json::Value* plist;//&lobbyinf["players"]["plist"]
    unordered_map<string, Json::Value*> lobby_players;//lobby角色的json

    void buildGamePlist();

    enum Player::ICON popValidIcon();
    vector<bool> iconlist;

    int readys;

    const string startmsg = "{\"type\":1}";

    void initGame();
    void gameStart();
    void refreshGame();//For Restart
    void nextRound();
    int cur_round;
    
    unordered_map<string, Json::Value*> game_players;//角色的json
    unordered_set<string> player_choice;//记录本轮哪些人已经选了（每次更新的临时变量）
    unordered_set<string> stay_list;
    
    vector<int> artifacts;//记录本回合出现的artifacts卡
    void next();
    //void distribute();
    vector<string> back_list;//记录本次返回名单（每次更新的临时变量）

    int hazard_record[5];

    

};

#endif // !GAMEROOM_H

